/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann - Tübingen, Germany 
 * Niko Schenk - Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package saphre.commands;

import saphre.core.SaphreCommandInterpreter;
import util.Interval;
import saphre.core.SuffixArray;
import collectors.impl.CollectLevelIntervals;
import saphre.core.Store;
import util.LinkedInterval;
import collectors.impl.visitors.CollectTraversal;

import java.util.List;
import java.util.Set;

public class CmdSuffLink {
    
    private final SaphreCommandInterpreter saphreCommandInterpreter;
    Interval[] sufflinks;

    public CmdSuffLink(SaphreCommandInterpreter saphreCommandInterpreter) {
        this.saphreCommandInterpreter = saphreCommandInterpreter;
    }

    public void CmdSuffLink(Store store, int N, SuffixArray sa, Interval top) {
        CollectLevelIntervals collect =
                new CollectLevelIntervals(store, sa, top);
        CollectTraversal p =
                new CollectTraversal(store, sa, collect, top);
        p.traverse();
        sufflinks = new Interval[N];
        List<List<Interval>> lList = collect.getlList();
        List<Set<LinkedInterval>> nList = collect.getnList();
        System.err.println(nList.size() + " " + lList.size());
        for (int i = 2; i < nList.size(); i++) {
            Set<LinkedInterval> nListRow =
                    nList.get(i);
            List<Interval> lListRow =
                    lList.get(i - 1);
            int li = 0;
            Interval candidate = lListRow.get(li);
            for (LinkedInterval psi : nListRow) {
                System.err.println(psi);
                while (candidate.rb() < psi.inter.rb()) {
                    li++;
                    candidate = lListRow.get(li);
                }
                System.out.println("cand " + candidate);
                sufflinks[psi.link] = candidate;
            }
        }
    }
}